import java.util.*;

class Student {
    private int id;//学生唯一标识
    private String name;//学生姓名
    private double score;//学生成绩
    private String major;//学生专业

    public Student(int id, String name, double score, String major) {
        this.id = id;
        this.name = name;
        this.score = score;
        this.major = major;
    }//构造函数

    public int getId() {
        return id;
    }//获取id

    public String getName() {
        return name;
    }//获取姓名

    public double getScore() {
        return score;
    }//获取分数

    public String getMajor() {
        return major;
    }//获取专业

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass()!= o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                ", major='" + major + '\'' +
                '}';
    }

    public void setScore(int score) {
        this.score = score;
    }
}

public class Homework_7_StuManage {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student(1, "Alice", 85.0, "Computer Science"));
        students.add(new Student(2, "Bob", 45.0, "Mathematics"));
        students.add(new Student(3, "Charlie", 78.5, "Physics"));
        students.add(new Student(4, "David", 59.0, "Computer Science"));
        students.add(new Student(5, "Eve", 93.0, "Mathematics"));
        students.add(new Student(6, "Frank", 70.0, "Physics"));
        students.add(new Student(2, "Duplicate Bob", 45.0, "Mathematics"));
        students.add(new Student(7, "Grace", 88.5, "Computer Science"));

        System.out.println("初始数据:");
        for (Student student : students) {
            System.out.println(student);
        }

        HashSet<Student> passedStudents = new HashSet<>();
        for (Student student : students) {
            if (student.getScore() >= 60) {
                passedStudents.add(student);
            }
        }
        System.out.println("\n及格学生信息:");
        for (Student student : passedStudents) {
            System.out.println(student);
        }//筛选及格学生

        HashMap<String, List<Student>> majorGroups = new HashMap<>();
        for (Student student : students) {
            String major = student.getMajor();
            if (!majorGroups.containsKey(major)) {
                majorGroups.put(major, new ArrayList<>());
            }
            majorGroups.get(major).add(student);
        }
        System.out.println("\n分组信息:");
        for (Map.Entry<String, List<Student>> entry : majorGroups.entrySet()) {
            String major = entry.getKey();
            List<Student> majorStudents = entry.getValue();
            System.out.println("专业: " + major);
            for (Student student : majorStudents) {
                System.out.println(student);
            }
            //计算平均成绩
            double sum = 0;
            for (Student student : majorStudents) {
                sum += student.getScore();
            }
            double average = sum / majorStudents.size();
            System.out.println("平均成绩: " + average);
            System.out.println("----------------");
        }//分专业id排序

        Collections.sort(students, (s1, s2) -> {
            if (s1.getScore()!= s2.getScore()) {
                return Double.compare(s2.getScore(), s1.getScore());
            } else {
                return s1.getName().compareTo(s2.getName());
            }
        });
        System.out.println("\n排序结果:");
        for (Student student : students) {
            System.out.println(student);
        }
        //分数排序，降序

        HashMap<Integer, Student> studentMap = new HashMap<>();
        for (Student student : students) {
            studentMap.put(student.getId(), student);
        }
        int searchId = 3;
        if (studentMap.containsKey(searchId)) {
            Student foundStudent = studentMap.get(searchId);
            foundStudent.setScore(95);
            System.out.println("\n更新后的学生信息: " + foundStudent);
        } else {
            System.out.println("\n该学生不存在");
        }
        //更新学生信息

        Student maxScoreStudent = students.get(0);
        int passedCount = 0;
        int failedCount = 0;
        for (Student student : students) {
            if (student.getScore() >= 60) {
                passedCount++;
            } else {
                failedCount++;
            }
            if (student.getScore() > maxScoreStudent.getScore()) {
                maxScoreStudent = student;
            }
        }
        System.out.println("\n最高成绩的学生信息: " + maxScoreStudent);
        System.out.println("及格学生总数: " + passedCount);
        System.out.println("不及格学生总数: " + failedCount);
    }
}